//
//  AddViewController.m
//  18-通讯录sqlite
//
//  Created by dllo on 16/8/8.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "AddViewController.h"
#import "AddModel.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random() % 256 / 255.0 )green:(arc4random() % 256 / 255.0 ) blue:(arc4random() % 256 / 255.0 ) alpha:1.0];
@interface AddViewController ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UITextField *textField;
@property(nonatomic, strong)UIImageView *imageView;
@property(nonatomic, strong)UIButton *button;
@property (nonatomic, strong) AddModel *modelStu;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLORAUTO;
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    textField.layer.borderWidth = 2.0;
    textField.layer.cornerRadius = 25;
    textField.placeholder = @"请输入name";
    textField.font = [UIFont systemFontOfSize:30];
    textField.textColor = COLORAUTO;
    [self.view addSubview:textField];
    self.textField = textField;
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(110,250 , 200, 200)];
    self.imageView.layer.borderWidth = 1;
    self.imageView.layer.cornerRadius = 100;
    self.imageView.layer.masksToBounds = YES;
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.modelStu.photo]];
    [self.view addSubview:self.imageView];
    [self creatBtnTwo];

}
- (void)creatBtnTwo{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    btn.backgroundColor = [UIColor whiteColor];
    
    btn.frame = CGRectMake(100, HEIGHT * 0.7,
                           100, 100);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitle:@"保存" forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(clickBtn:  ) forControlEvents:UIControlEventTouchUpInside];
    
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"去系统相册" forState:UIControlStateNormal];
    [btn setTitle:@"去系统相册" forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(selctImage:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(200,HEIGHT * 0.7,
                           150, 100);
    //裁成边角
    btn.layer.cornerRadius = 100;
    btn.layer.masksToBounds = YES;
    [self.view addSubview:btn];
}
- (void)clickBtn:(UIButton *)sender{
    AddModel *model = [[AddModel alloc] init];
    if (self.textField.text.length > 1) {
        model.name = self.textField.text;
        [self.delegate addModel:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)selctImage:(UIButton *)sender{
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    pickerController.delegate = self;
    //设置选取照片是否可以编辑
    pickerController.allowsEditing = YES;
    //  设置相册呈现样式
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //照片的选取样式还有以下两种
    //     pickerController.sourceType =   UIImagePickerControllerSourceTypePhotoLibrary;
    //    直接全部呈现系统相册
    //    pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //    //调取摄像头
    pickerController.delegate = self;
    [self.navigationController presentViewController:pickerController animated:YES completion:^{
        
        
    }];
    
}
//UIimagePickerController的协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{
    self.imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //这是捕获点击右上角cancel按钮所触发的事件，如果我们需要在点击cancel按钮的时候做一些其他逻辑操作。就需要实现该代理方法，如果不做任何逻辑操作，就可以不实现
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
